package com.reservation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reservation.common.Result;
import com.reservation.entity.Exhibition;
import com.reservation.service.ExhibitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 展览控制器
 */
@RestController
@RequestMapping("/exhibition")
public class ExhibitionController {

    @Autowired
    private ExhibitionService exhibitionService;

    /**
     * 添加展览（仅管理员可用）
     *
     * @param exhibition 展览信息
     * @return 添加结果
     */
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Exhibition> addExhibition(@RequestBody @Valid Exhibition exhibition) {
        Exhibition addedExhibition = exhibitionService.addExhibition(exhibition);
        return Result.success(addedExhibition, "添加成功");
    }

    /**
     * 修改展览信息（仅管理员可用）
     *
     * @param exhibition 展览信息
     * @return 修改结果
     */
    @PutMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Exhibition> updateExhibition(@RequestBody @Valid Exhibition exhibition) {
        Exhibition updatedExhibition = exhibitionService.updateExhibition(exhibition);
        return Result.success(updatedExhibition, "修改成功");
    }

    /**
     * 获取展览详情
     *
     * @param id 展览ID
     * @return 展览信息
     */
    @GetMapping("/{id}")
    public Result<Exhibition> getExhibitionById(@PathVariable Long id) {
        Exhibition exhibition = exhibitionService.getExhibitionById(id);
        return Result.success(exhibition);
    }

    /**
     * 删除展览（仅管理员可用）
     *
     * @param id 展览ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> deleteExhibition(@PathVariable Long id) {
        boolean result = exhibitionService.deleteExhibition(id);
        return Result.success(null, "删除成功");
    }

    /**
     * 分页查询展览列表
     *
     * @param pageNum  当前页
     * @param pageSize     每页大小
     * @param name     展览名称（可选）
     * @param status   展览状态（可选）
     * @param location 展览地点（可选）
     * @return 展览列表
     */
    @GetMapping("/list")
    public Result<IPage<Exhibition>> getExhibitionList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String location) {
        Page<Exhibition> page = new Page<>(pageNum, pageSize);
        IPage<Exhibition> exhibitionPage = exhibitionService.getExhibitionList(page, name, status, location);
        return Result.success(exhibitionPage);
    }

    /**
     * 获取所有展览
     *
     * @return 展览列表
     */
    @GetMapping("/all")
    public Result<List<Exhibition>> getAllExhibitions() {
        List<Exhibition> exhibitions = exhibitionService.getAllExhibitions();
        return Result.success(exhibitions);
    }

    /**
     * 获取正在进行的展览
     *
     * @return 展览列表
     */
    @GetMapping("/ongoing")
    public Result<List<Exhibition>> getOngoingExhibitions() {
        List<Exhibition> exhibitions = exhibitionService.getOngoingExhibitions();
        return Result.success(exhibitions);
    }

    /**
     * 获取即将开始的展览
     *
     * @return 展览列表
     */
    @GetMapping("/upcoming")
    public Result<List<Exhibition>> getUpcomingExhibitions() {
        List<Exhibition> exhibitions = exhibitionService.getUpcomingExhibitions();
        return Result.success(exhibitions);
    }

    /**
     * 更新展览状态（仅管理员可用）
     *
     * @param id     展览ID
     * @param status 展览状态
     * @return 更新结果
     */
    @PutMapping("/{id}/status")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Exhibition> updateExhibitionStatus(
            @PathVariable Long id,
            @RequestParam String status) {
        Exhibition exhibition = exhibitionService.updateExhibitionStatus(id, status);
        return Result.success(exhibition, "状态更新成功");
    }

    /**
     * 检查日期是否可预约
     *
     * @param exhibitionId 展览ID
     * @param date         预约日期
     * @return 是否可预约
     */
    @GetMapping("/check-availability")
    public Result<Boolean> checkDateAvailable(
            @RequestParam Long exhibitionId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
        boolean available = exhibitionService.checkDateAvailable(exhibitionId, date);
        return Result.success(available);
    }

    /**
     * 获取某日期的预约人数
     *
     * @param exhibitionId 展览ID
     * @param date         预约日期
     * @return 已预约人数
     */
    @GetMapping("/reservation-count")
    public Result<Integer> getReservationCount(
            @RequestParam Long exhibitionId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
        int count = exhibitionService.getReservationCount(exhibitionId, date);
        return Result.success(count);
    }
}